<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Models\Ad;
use Illuminate\Http\Request;

use App\Http\Models\Member;
use App\Http\Models\Administrator;

class ManagerController extends AdminBaseController{

    public function __construct(Request $request){
        parent::__construct($request);
        $this->param['active'] = 'manager';
    }

    public function index(){

        $administrators = Administrator::orderBy("id","desc")->paginate(20);
        $this->param['data'] = $administrators;

        return view("admin.manager.index",$this->param);
    }


    public function edit($id){

        $administrator = Administrator::where("id",$id)->first();

        $this->param['content'] = $administrator;

        return view("admin.manager.edit",$this->param);
    }

    public function update(Request $request,$id)
    {
        $content = Administrator::find($id);

        $username = $request->input('username');
        $password = $request->input('password');
        if(!$username ){
            return $this->failure("/administrator/manager/".$id."/edit","账号不能为空");
        }
        //判读重复
        $count = Administrator::where('username',$username)->where("id","<>",$id)->count();
        if($count){
            return $this->failure("/administrator/manager/".$id."/edit","该账号名称已经存在了，不能重复");
        }

        $content->username = $username;
        if($password){
            $content->password = md5($password);
        }

        $content->save();

        return redirect('/administrator/manager');
    }
}